home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / zcontext.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  13KB  |  517 lines

  1. /* Copyright (C) 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zcontext.c */
  20. /* Display PostScript context operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "dict.h"
  27. #include "dstack.h"
  28. #include "estack.h"
  29. #include "state.h"
  30. #include "store.h"
  31.  
  32. /****** THIS FILE IS NOT IN GOOD ENOUGH SHAPE TO USE YET. ******/
  33.  
  34. /* Procedure hooks in interp.c */
  35. extern int (*gs_interp_reschedule_proc)(P0());
  36. extern int (*gs_interp_time_slice_proc)(P0());
  37.  
  38. /* Context structure */
  39. typedef enum {
  40.     cs_invalid,
  41.     cs_active,
  42.     cs_done
  43. } ctx_status;
  44. typedef struct gs_context_s {
  45.     ctx_status status;
  46.     long index;
  47.     int detach;            /* true if a detach has been */
  48.                     /* executed for this context */
  49.     struct gs_context_s *next;    /* next context with same status */
  50.                     /* (active, waiting on same lock, */
  51.                     /* waiting on same condition) */
  52.     struct gs_context_s *joiner;    /* context waiting on a join */
  53.                     /* for this one */
  54.     struct gs_context_s *table_next;    /* hash table chain */
  55.         /* Externally visible context state */
  56.     ref stacks;            /* t_array */
  57. #define default_stacksize 50
  58.     uint ossize;
  59.     uint essize;
  60.     uint dssize;
  61.     gs_state *pgs;
  62.     /****** MORE STUFF HERE ******/
  63. } gs_context;
  64.  
  65. /* Context list structure */
  66. typedef struct ctx_list_s {
  67.     gs_context *head;
  68.     gs_context *tail;
  69. } ctx_list;
  70.  
  71. /* Condition structure */
  72. typedef struct gs_condition_s {
  73.     ctx_list waiting;    /* contexts waiting on this condition */
  74. } gs_condition;
  75.  
  76. /* Lock structure */
  77. typedef struct gs_lock_s {
  78.     ctx_list waiting;    /* contexts waiting for this lock */
  79.     gs_context *holder;        /* context holding the lock, if any */
  80. } gs_lock;
  81.  
  82. /* Global state */
  83. private gs_context *ctx_current;
  84. private ctx_list active;
  85. #define ctx_table_size 19
  86. private gs_context *ctx_table[ctx_table_size];
  87. private long ctx_next_index;
  88.  
  89. /* Forward references */
  90. private int context_create(P2(uint, gs_context **));
  91. private int context_param(P2(os_ptr, gs_context **));
  92. #define check_context(op, vpc)\
  93.   if ( (code = context_param(op, &vpc)) < 0 ) return code
  94. private void context_destroy(P1(gs_context *));
  95. private int lock_acquire(P1(os_ptr));
  96. private int lock_release(P1(os_ptr));
  97.  
  98. /* List manipulation macros */
  99. #define add_last(pl,pc)\
  100.   (((pl)->head == 0 ? ((pl)->head = pc) : ((pl)->tail->next = pc)),\
  101.    (pl)->tail = pc, (pc)->next = 0)
  102. #define add_last_all(pl,pcl)        /* pcl->head != 0 */\
  103.   (((pl)->head == 0 ? ((pl)->head = (pcl)->head) :\
  104.     ((pl)->tail->next = (pcl)->head)),\
  105.    (pl)->tail = (pcl)->tail, (pcl)->head = 0)
  106.  
  107. /* ------ Initialization ------ */
  108.  
  109. private int ctx_reschedule(P0());
  110. private int ctx_time_slice(P0());
  111. private void
  112. zcontext_init(void)
  113. {    ctx_current = 0;
  114.     active.head = 0;
  115.     memset(ctx_table, 0, sizeof(ctx_table));
  116.     ctx_next_index = 1;
  117.     /* Create an initial context. */
  118.     context_create(default_stacksize, &ctx_current);
  119.     /* Hook into the interpreter. */
  120.     gs_interp_reschedule_proc = ctx_reschedule;
  121.     gs_interp_time_slice_proc = ctx_time_slice;
  122. }
  123.  
  124. /* ------ Interpreter interface to scheduler ------ */
  125.  
  126. /* When an operator decides it is time to run a new context, */
  127. /* it returns o_reschedule.  The interpreter saves all its state in */
  128. /* memory, calls ctx_reschedule, and then loads the state from memory. */
  129. private int
  130. ctx_reschedule(void)
  131. {    register gs_context *pctx;
  132.     ref *stkp;
  133.     uint ossize, essize, dssize;
  134.     /* Save the state of the current context in ctx_current, */
  135.     /* if any context is current at all. */
  136.     pctx = ctx_current;
  137.     if ( pctx != 0 )
  138.        {    uint stackneed;
  139.         int code;
  140.         ref newstacks;
  141.         ossize = osp - osbot + 1;
  142.         essize = esp - esbot + 1;
  143.         dssize = dsp - dsbot + 1;
  144.         stackneed = ossize + essize + dssize;
  145.         if ( stackneed > r_size(&pctx->stacks) )
  146.            {    alloc_free_array(&pctx->stacks, "ctx_reschedule");
  147.             code = alloc_array(&newstacks, 0, stackneed,
  148.                        "ctx_reschedule");
  149.             if ( code < 0 )
  150.                {    /* Punt. */
  151.                 lprintf("Can't allocate stacks!");
  152.                 return_error(e_Fatal);
  153.                }
  154.             pctx->stacks = newstacks;
  155.            }
  156.         stkp = pctx->stacks.value.refs;
  157. #define save_stack(sbot, ssize)\
  158.   memcpy(stkp, sbot, ssize * sizeof(ref));\
  159.   pctx->ssize = ssize;\
  160.   stkp += ssize
  161.         save_stack(osbot, ossize);
  162.         save_stack(esbot, essize);
  163.         save_stack(dsbot, dssize);
  164. #undef save_stack
  165.         pctx->pgs = igs;
  166.         /****** MORE TO DO HERE ******/
  167.        }
  168.     /* Run the first ready context. */
  169.     if ( active.head == 0 )
  170.        {    lprintf("No context to run!");
  171.         return_error(e_Fatal);
  172.        }
  173.     ctx_current = active.head;
  174.     active.head = active.head->next;
  175.     /* Load the state of the new current context. */
  176.     pctx = ctx_current;
  177.     stkp = pctx->stacks.value.refs;
  178. #define reload_stack(sbot, ssize, sp)\
  179.   ssize = pctx->ssize;\
  180.   memcpy(sbot, stkp, ssize * sizeof(ref));\
  181.   sp = sbot + (ssize - 1);\
  182.   stkp += ssize
  183.     reload_stack(osbot, ossize, osp);
  184.     reload_stack(esbot, essize, esp);
  185.     esfile = 0;
  186.     reload_stack(dsbot, dssize, dsp);
  187. #undef reload_stack
  188.     igs = pctx->pgs;
  189.     /****** MORE TO DO HERE ******/
  190.     return 0;
  191. }
  192.  
  193. /* If the interpreter wants to time-slice, it saves its state, */
  194. /* calls ctx_time_slice, and reloads its state. */
  195. private int
  196. ctx_time_slice(void)
  197. {    if ( active.head == 0 ) return 0;
  198.     add_last(&active, ctx_current);
  199.     return ctx_reschedule();
  200. }
  201.  
  202. /* ------ Context operators ------ */
  203.  
  204. private int fork_done(P1(os_ptr));
  205.  
  206. /* - currentcontext <context> */
  207. int
  208. zcurrentcontext(register os_ptr op)
  209. {    push(1);
  210.     make_int(op, ctx_current->index);
  211.     return 0;
  212. }
  213.  
  214. /* <context> detach - */
  215. int
  216. zdetach(register os_ptr op)
  217. {    gs_context *pctx;
  218.     int code;
  219.     check_context(op, pctx);
  220.     if ( pctx->joiner != 0 || pctx->detach )
  221.         return_error(e_invalidcontext);
  222.     pop(1);
  223.     switch ( pctx->status )
  224.        {
  225.     case cs_active:
  226.         pctx->detach = 1;
  227.         break;
  228.     case cs_done:
  229.         context_destroy(pctx);
  230.         if ( pctx == ctx_current )
  231.            {    ctx_current = 0;
  232.             return o_reschedule;
  233.            }
  234.        }
  235.     return 0;
  236. }
  237.  
  238. /* <mark> <obj1> ... <objN> <proc> fork <context> */
  239. int
  240. zfork(register os_ptr op)
  241. {    os_ptr mp = op - 1;
  242.     gs_context *pctx;
  243.     uint ossize, essize, dssize, stacksize;
  244.     int code;
  245.     ref *stkp;
  246.     check_proc(*op);
  247.     while ( !r_has_type(mp, t_mark) )
  248.     {    if ( mp <= osbot )
  249.             return_error(e_unmatchedmark);
  250.         mp--;
  251.     }
  252.     ossize = op - mp - 1;
  253.     essize = 2;
  254.     dssize = dsp - dsbot + 1;
  255.     stacksize = ossize + essize + dssize + 10;
  256.     code = context_create(stacksize, &pctx);
  257.     if ( code < 0 ) return code;
  258.     stkp = pctx->stacks.value.refs;
  259.     pctx->ossize = ossize;
  260.     memcpy(stkp, mp + 1, ossize * sizeof(ref));
  261.     stkp += ossize;
  262.     pctx->essize = essize;
  263.     make_oper(stkp, 0, fork_done);
  264.     stkp++;
  265.     *stkp = *op;
  266.     stkp++;
  267.     pctx->dssize = dssize;
  268.     memcpy(stkp, dsbot, dssize * sizeof(ref));
  269.     pctx->pgs = igs;        /* ****** WRONG, MUST COPY ****** */
  270.     /****** MORE INIT HERE? ******/
  271.     add_last(&active, pctx);
  272.     osp = mp;
  273.     make_int(mp, pctx->index);
  274.     return 0;
  275. }
  276. /* This gets executed when a context terminates normally. */
  277. /****** HOW TO GET IT EXECUTED ON ERROR TERMINATION? ******/
  278. private int
  279. fork_done(os_ptr op)
  280. {    if ( ctx_current->detach )
  281.        {    context_destroy(ctx_current);
  282.         ctx_current = 0;
  283.        }
  284.     else
  285.        {    gs_context *pctx = ctx_current->joiner;
  286.         ctx_current->status = cs_done;
  287.         /* Schedule the context waiting to join this one, if any. */
  288.         if ( pctx != 0 ) add_last(&active, pctx);
  289.        }
  290.     return o_reschedule;
  291. }
  292.  
  293. /* <context> join <mark> <obj1> ... <objN> */
  294. int
  295. zjoin(register os_ptr op)
  296. {    gs_context *pctx;
  297.     int code;
  298.     check_context(op, pctx);
  299.     if ( pctx->joiner != 0 || pctx == ctx_current || pctx->detach )
  300.         return_error(e_invalidcontext);
  301.     switch ( pctx->status )
  302.        {
  303.     case cs_active:
  304.         pctx->joiner = ctx_current;
  305.         return o_reschedule;
  306.     case cs_done:
  307.        {    uint count = pctx->ossize;
  308.         os_ptr mp = op;
  309.         push(count);
  310.         make_mark(mp);
  311.         memcpy(++mp, pctx->stacks.value.refs, count * sizeof(ref));
  312.         context_destroy(pctx);
  313.        }
  314.        }
  315.     return 0;
  316. }
  317.  
  318. /* - yield - */
  319. int
  320. zyield(register os_ptr op)
  321. {    if ( active.head == 0 ) return 0;
  322.     add_last(&active, ctx_current);
  323.     return o_reschedule;
  324. }
  325.  
  326. /* ------ Condition and lock operators ------ */
  327.  
  328. private int
  329.   monitor_release(P1(os_ptr)),
  330.   await_lock(P1(os_ptr));
  331.  
  332. /* - condition <condition> */
  333. int
  334. zcondition(register os_ptr op)
  335. {    gs_condition *pcond =
  336.         (gs_condition *)alloc(1, sizeof(gs_condition), "zcondition");
  337.     if ( pcond == 0 )
  338.         return_error(e_VMerror);
  339.     pcond->waiting.head = 0;
  340.     push(1);
  341.     make_tv(op, t_condition, pcond, pcond);
  342.     return 0;
  343. }
  344.  
  345. /* - lock <lock> */
  346. int
  347. zlock(register os_ptr op)
  348. {    gs_lock *plock = (gs_lock *)alloc(1, sizeof(gs_lock), "zlock");
  349.     if ( plock == 0 )
  350.         return_error(e_VMerror);
  351.     plock->holder = 0;
  352.     plock->waiting.head = 0;
  353.     push(1);
  354.     make_tv(op, t_lock, plock, plock);
  355.     return 0;
  356. }
  357.  
  358. /* <lock> <proc> monitor - */
  359. int
  360. zmonitor(register os_ptr op)
  361. {    gs_lock *plock;
  362.     int code;
  363.     check_type(op[-1], t_lock);
  364.     check_proc(*op);
  365.     plock = op[-1].value.plock;
  366.     check_estack(2);
  367.     if ( plock->holder == ctx_current )
  368.         return_error(e_invalidcontext);
  369.     code = lock_acquire(op - 1);
  370.     /****** HOW TO GUARANTEE RELEASE IF CONTEXT DIES? ******/
  371.     push_op_estack(monitor_release);
  372.     *++esp = op[-1];
  373.     pop(2);
  374.     return code;
  375. }
  376. /* Release the monitor lock when the procedure completes. */
  377. private int
  378. monitor_release(os_ptr op)
  379. {    es_ptr ep = esp--;
  380.     return lock_release(ep);
  381. }
  382.  
  383. /* <condition> notify - */
  384. int
  385. znotify(register os_ptr op)
  386. {    gs_condition *pcond;
  387.     check_type(*op, t_condition);
  388.     pcond = op->value.pcond;
  389.     pop(1); op--;
  390.     if ( pcond->waiting.head == 0 ) return 0;    /* nothing to do */
  391.     add_last_all(&active, &pcond->waiting);
  392.     return zyield(op);
  393. }
  394.  
  395. /* <lock> <condition> wait - */
  396. int
  397. zwait(register os_ptr op)
  398. {    gs_condition *pcond;
  399.     check_type(op[-1], t_lock);
  400.     check_type(*op, t_condition);
  401.     pcond = op->value.pcond;
  402.     check_estack(1);
  403.     lock_release(op - 1);
  404.     add_last(&pcond->waiting, ctx_current);
  405.     push_op_estack(await_lock);
  406.     return o_reschedule;
  407. }
  408. /* When the condition is signaled, wait for acquiring the lock. */
  409. private int
  410. await_lock(os_ptr op)
  411. {    int code = lock_acquire(op - 1);
  412.     pop(2);
  413.     return code;
  414. }
  415.  
  416. /* ------ Internal routines ------ */
  417.  
  418. /* Create a context. */
  419. private int
  420. context_create(uint stacksize, gs_context **ppctx)
  421. {    gs_context *pctx;
  422.     int code;
  423.     long ctx_index;
  424.     gs_context **pte;
  425.     pctx = (gs_context *)alloc(1, sizeof(gs_context), "context");
  426.     if ( pctx == 0 )
  427.         return_error(e_VMerror);
  428.     if ( stacksize < default_stacksize ) stacksize = default_stacksize;
  429.     code = alloc_array(&pctx->stacks, 0, stacksize, "context(stacks)");
  430.     if ( code < 0 ) return code;
  431.     ctx_index = ctx_next_index++;
  432.     pctx->status = cs_active;
  433.     pctx->index = ctx_index;
  434.     pctx->detach = 0;
  435.     pctx->next = 0;
  436.     pctx->joiner = 0;
  437.     pte = &ctx_table[ctx_index % ctx_table_size];
  438.     pctx->table_next = *pte;
  439.     *pte = pctx;
  440.     *ppctx = pctx;
  441.     return 0;
  442. }
  443.  
  444. /* Check a context ID.  Note that we do not check for context validity. */
  445. private int
  446. context_param(os_ptr op, gs_context **ppctx)
  447. {    gs_context *pctx;
  448.     long index;
  449.     check_type(*op, t_integer);
  450.     index = op->value.intval;
  451.     if ( index < 0 )
  452.         return_error(e_invalidcontext);
  453.     pctx = ctx_table[index % ctx_table_size];
  454.     for ( ; ; pctx = pctx->table_next )
  455.     {    if ( pctx == 0 )
  456.             return_error(e_invalidcontext);
  457.         if ( pctx->index == index ) break;
  458.     }
  459.     *ppctx = pctx;
  460.     return 0;
  461. }
  462.  
  463. /* Destroy a context. */
  464. private void
  465. context_destroy(gs_context *pctx)
  466. {    gs_context **ppctx = &ctx_table[pctx->index % ctx_table_size];
  467.     while ( *ppctx != pctx )
  468.         ppctx = &(*ppctx)->table_next;
  469.     *ppctx = (*ppctx)->table_next;
  470.     alloc_free_array(&pctx->stacks, "context_destroy");
  471.     alloc_free((char *)pctx, 1, sizeof(gs_context), "context_destroy");
  472. }
  473.  
  474. /* Acquire a lock.  Return 0 if acquired, o_reschedule if not. */
  475. private int
  476. lock_acquire(os_ptr op)
  477. {    gs_lock *plock = op->value.plock;
  478.     if ( plock->holder == 0 )
  479.        {    plock->holder = ctx_current;
  480.         return 0;
  481.        }
  482.     add_last(&plock->waiting, ctx_current);
  483.     return o_reschedule;
  484. }
  485.  
  486. /* Release a lock.  Return 0 if OK, e_invalidcontext if not. */
  487. private int
  488. lock_release(os_ptr op)
  489. {    gs_lock *plock = op->value.plock;
  490.     if ( plock->holder == ctx_current )
  491.        {    plock->holder = 0;
  492.         add_last_all(&active, &plock->waiting);
  493.         return 0;
  494.        }
  495.     return_error(e_invalidcontext);
  496. }
  497.  
  498. /* ------ Initialization procedure ------ */
  499.  
  500. op_def zcontext_op_defs[] = {
  501.     {"0condition", zcondition},
  502.     {"0currentcontext", zcurrentcontext},
  503.     {"1detach", zdetach},
  504.     {"2fork", zfork},
  505.     {"1join", zjoin},
  506.     {"0lock", zlock},
  507.     {"2monitor", zmonitor},
  508.     {"1notify", znotify},
  509.     {"2wait", zwait},
  510.     {"0yield", zyield},
  511.         /* Internal operators */
  512.     {"0%fork_done", fork_done},
  513.     {"2%monitor_release", monitor_release},
  514.     {"2%await_lock", await_lock},
  515.     op_def_end(zcontext_init)
  516. };
  517.